package cn.topcodes.tcsf.admin.web.interceptor;

import cn.topcode.unicorn.utils.CollectionUtil;
import cn.topcode.unicorn.utils.IpUtil;
import cn.topcodes.tcsf.admin.service.log.AccessLogService;
import cn.topcodes.tcsf.admin.service.log.SysOperateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import cn.topcodes.tcsf.admin.domain.entity.SysAccessLog;
import cn.topcodes.tcsf.admin.service.SystemPropertyService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 访问日志拦截器
 * @author Unicorn
 *
 */
public class AccessLogInterceptor extends HandlerInterceptorAdapter {
	
	public static final String REQUEST_TIME_KEY = "_Request_Time";
	
	@Autowired
	private AccessLogService accessLogService;

	@Autowired
	private SysOperateLogService operateLogService;
	
	@Autowired
	private SystemPropertyService propService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		operateLogService.readyLog();
		operateLogService.setIp(IpUtil.getIp(request));

		if(propService.isAccessLogOn()) {

			//	处理请求前记录请求到达时间
			request.setAttribute(REQUEST_TIME_KEY, new Date());
		}
		
		return super.preHandle(request, response, handler);
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
		if(propService.isAccessLogOn()) {
			SysAccessLog accessLog = new SysAccessLog();
			accessLog.setUrl(request.getRequestURI());
			accessLog.setMethod(request.getMethod());
			accessLog.setIp(IpUtil.getIp(request));
			
			//	处理请求后记录响应时间及耗费毫秒数
			Date requestTime = (Date) request.getAttribute(REQUEST_TIME_KEY);
			Date responseTime = new Date();
			int consuming = (int) (responseTime.getTime() - requestTime.getTime());
			accessLog.setRequestTime(requestTime);
			accessLog.setResponseTime(responseTime);
			accessLog.setConsuming(consuming);
			
			String requestHeader = handleRequestHeader(request);
			accessLog.setRequestHead(requestHeader);
			
			String responseHeader = handleResponseHeader(response);
			accessLog.setResponseHead(responseHeader);
			
			/*request.getInputStream().reset();*/
			/*String requestBody = IOUtils.toString(request.getInputStream(),Charset.forName("utf-8"));
			accessLog.setRequestBody(requestBody);
			response.get
			accessLog.setResponseBody();*/
			
			this.accessLogService.create(accessLog);
		}
	}
	
	/*将请求头部转换为键值对形式的字符串*/
	private String handleRequestHeader(HttpServletRequest request) {
		List<String> headers = new ArrayList<String>();
		Enumeration<String> requestHeader = request.getHeaderNames();
		while(requestHeader.hasMoreElements()) {
			String header = requestHeader.nextElement();
			String value = request.getHeader(header);
			headers.add(header + ":" + value);
		}
		String headerStr = CollectionUtil.array2String(headers);
		return headerStr;
	}
	
	/*将响应头部转换为键值对形式的字符串*/
	private String handleResponseHeader(HttpServletResponse response) {
		List<String> headers = new ArrayList<String>();
		Collection<String> responseHeader = response.getHeaderNames();
		for(String header : responseHeader) {
			String value = response.getHeader(header);
			headers.add(header + ":" + value);
		}
		String headerStr = CollectionUtil.array2String(headers);
		return headerStr;
	}

}
